//引入mockjs
import Mock from "mockjs";

//引入JSON数据格式
import mockList from "./mockList.json";
import parkingList from "./parkingList.json";
import parkingList2 from "./parkingList2.json";
import login from "./login.json";
import trafficData from "./trafficData.json";

//mock数据  参数：请求地址，请求方法，请求数据
Mock.mock("/mock/mockList", "get", { code: 200, data: mockList });
Mock.mock("/mock/parkingList", "get", { code: 200, data: parkingList });
Mock.mock("/mock/parkingList2", "get", { code: 200, data: parkingList2 });
Mock.mock("/mock/trafficData", "get", { code: 200, data: trafficData });

// Mock 登录接口
Mock.mock("/mock/login", "post", (options) => {
  // 处理请求体，解析 JSON
  const body = JSON.parse(options.body || "{}"); // options.body 是字符串，需解析
  const {
    username = "",
    password = "",
    phone = "",
    verificationCode = "",
    loginType = "",
  } = body.params || {}; // 从 body.params 中获取
  console.log(options.body);
  if (loginType === "account") {
    console.log(
      `Received login request with username: ${username}, password: ${password}`
    );
  } else if (loginType === "phone") {
    console.log(
      `Received login request with phone: ${phone}, verificationCode: ${verificationCode}`
    );
  }

  // 这里可以根据 username 和 password 来进行不同的响应
  if (
    (username && password && loginType === "account") ||
    (phone && verificationCode && loginType === "phone")
  ) {
    // 返回成功的登录数据
    return {
      code: 200,
      data: login,
      status: "success",
    };
  } else {
    // 返回失败的登录数据
    return {
      code: 400,
      message: "用户名或密码错误",
      status: "fail",
    };
  }
});

// 添加日志以确认 Mock 服务注册成功
console.log("创建mock服务器：mockingList");
